import { PrismaService } from '@app/prisma';
import { ClassesEntity } from '@app/prisma/classes.entity/classes.entity';
import { CollegeEntity } from '@app/prisma/college.entity/college.entity';
import { DepartmentEntity } from '@app/prisma/department.entity/department.entity';
import { ScienceEntity } from '@app/prisma/science.entity/science.entity';
import { Injectable } from '@nestjs/common';

@Injectable()
export class CommonService {
    constructor(
        private readonly prisma: PrismaService
    ) { }

    public async delColleges(
        colleges: CollegeEntity[]
    ) {
        const depts = colleges.map(e => e.Department || []).flat();
        await this.delDepartment(depts);
        await this.prisma.college.deleteMany({
            where: {
                id: {
                    in: colleges.map(e => e.id)
                }
            }
        })
    }
    public async delDepartment(
        depts: DepartmentEntity[]
    ) {
        const sciences = depts.map(e => e.Science || []).flat();
        await this.delSciences(sciences);
        await this.prisma.department.deleteMany({
            where: {
                id: {
                    in: depts.map(e => e.id)
                }
            }
        })
    }

    public async delSciences(
        sciences: ScienceEntity[]
    ) {
        await this.prisma.science.deleteMany({
            where: {
                id: {
                    in: sciences.map(e => e.id)
                }
            }
        })
    }
    public async delClasses(
        classes: ClassesEntity[]
    ) {
        await this.prisma.classes.deleteMany({
            where: {
                id: {
                    in: classes.map(e => e.id)
                }
            }
        })
    }
}
